package web

import (
	"github.com/gin-gonic/gin"
	"html/template"
	"net/http"
)

func Template(router *gin.Engine) {
	router.LoadHTMLGlob("./views/template/*")
	router.Static("/statics", "./views/statics/")

	funcMap := getCustomTemplateFunc()

	router.SetFuncMap(funcMap)

	router.GET("/raw", func(c *gin.Context) {
		c.HTML(http.StatusOK, "raw.tmpl", gin.H{})
	})

}

type TemplateFuncMap template.FuncMap

func (t TemplateFuncMap) Add(keyword string, f func(string) interface{}) {
	t[keyword] = f
}

// 添加自定义模板函数
func getCustomTemplateFunc() template.FuncMap {
	r := TemplateFuncMap{}

	r.Add("cDateFormat", func(s string) interface{} {
		return s
	})

	return template.FuncMap(r)
}
